import ajax from '../../common/js/ajax'
import router from '@ohos.router'
import data_storage from '@ohos.data.storage';
import featureAbility from '@ohos.ability.featureAbility'

export default {
    data: {
        navigationHeight: '300px',
        rootCategoryHeight: '160px',
        rootCategories: [],
        rootInActiveId: 1,
        subCategories: [],
        subInActiveId: 0,
        goodsList: [],
        flag: true,
        sortNewPrice: true,
        sortDiscount: true
    },
    onInit() {
        let categoryId
        try {
            categoryId = router.getParams()['categoryId']
        } catch (error) {
            categoryId = 1
        }
        this.rootInActiveId = categoryId
        this.ajaxGetRootCategories()
        this.ajaxGetSubCategories(this.rootInActiveId)
    },
    unfold() {
        this.flag = false
        this.navigationHeight = '1140px'
        this.rootCategoryHeight = '1000px'
    },
    collapse() {
        this.flag = true
        this.navigationHeight = '300px'
        this.rootCategoryHeight = '160px'
    },
    ajaxGetRootCategories() {
        ajax.get({
            url: 'http://124.71.79.186:8081/menu/findNav',
            success: resp => {
                if (resp.code === 200) {
                    this.rootCategories = resp.data
                    // console.log(JSON.stringify(this.rootCategories))
                }
            }
        })
    },
    ajaxGetSubCategories(id) {
        ajax.get({
            url: 'http://124.71.79.186:8081/menu/findNav?id=' + id,
            success: resp => {
                if (resp.code === 200) {
                    this.subCategories = resp.data
                    // console.log(JSON.stringify(this.subCategories))
                    this.subInActiveId = resp.data[0].id
                    // console.log(this.subInActiveId)
                    this.ajaxGetGoodsList(this.subInActiveId)
                }
            }
        })
    },
    ajaxGetGoodsList(typeId) {
        ajax.get({
            url: 'http://124.71.79.186:8081/commodity/findByType?typeId=' + typeId,
            success: resp => {
                if (resp.code === 200) {
                    this.goodsList = resp.data
                    //                    console.log(JSON.stringify(this.goodsList))
                }
            }
        })
    },
    toGoodsDetail(goodsId) {
        //        console.log(goodsId)
        router.push({
            url: 'pages/goodsDetails/goodsDetails',
            params: {
                goodsId
            },
        })

    },
    changeRootCategory(id) {
        this.rootInActiveId = id
        this.ajaxGetSubCategories(id)
    },
    changeSubCategory(id) {
        this.subInActiveId = id
        this.ajaxGetGoodsList(id)
    },
    sortNewPrice() {
        if (this.sortNewPrice) {
            this.goodsList = this.goodsList.sort((a, b) => a.nowPrice - b.nowPrice)
        } else {
            this.goodsList = this.goodsList.sort((a, b) => b.nowPrice - a.nowPrice)
        }
        this.sortNewPrice = !this.sortNewPrice
    },
    sortDiscount() {
        if (this.sortDiscount) {
            this.goodsList = this.goodsList.sort((a, b) => {
                let aOldPrice = a.oldPrice ? a.oldPrice : a.nowPrice
                let bOldPrice = b.oldPrice ? b.oldPrice : b.nowPrice
                const aDiscount = (aOldPrice - a.nowPrice) / a.oldPrice;
                const bDiscount = (bOldPrice - b.nowPrice) / b.oldPrice;
                return aDiscount - bDiscount;
            })
        } else {
            this.goodsList = this.goodsList.sort((a, b) => {
                let aOldPrice = a.oldPrice ? a.oldPrice : a.nowPrice
                let bOldPrice = b.oldPrice ? b.oldPrice : b.nowPrice
                const aDiscount = (aOldPrice - a.nowPrice) / a.oldPrice;
                const bDiscount = (bOldPrice - b.nowPrice) / b.oldPrice;
                return bDiscount - aDiscount;
            })
        }
        this.sortDiscount = !this.sortDiscount
    },
    addGoods(goods) {
        var context = featureAbility.getContext();
        context.getFilesDir().then((filePath) => {
            let storage = data_storage.getStorageSync(filePath + '/myStore')

            let goodsList = JSON.parse(storage.getSync('goodsList', JSON.stringify([])))
            const existingGoods = goodsList.find(g => g.id === goods.id)
            if (existingGoods) {
                existingGoods.buyNum++
            } else {
                goodsList.push({
                    ...goods,
                    buyNum: 1,
                    checked: false
                })
            }

            storage.putSync('goodsList', JSON.stringify(goodsList))
            storage.flushSync()

        });
    }
}
